﻿using System;
using System.Data;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;

namespace WebLogin
{
    public partial class _Default : Page
    {

        #region 加载页面
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {

            }
            else
            {
                Response.Write("<script>alert('欢迎来到内容管理系统')</script>");
                FillData();
            }
        }
        #endregion

        #region 数据传入
        private void FillData()
        {
            var sql = "select Id,Username,Password,IsActived,IsDeleted from Users";
            var dt = DbHelper.GetDataTable(sql);
            //绑定数据源
            GV1.DataSource = dt;
            //展示数据
            GV1.DataBind();

        }
        #endregion

        #region 获取数据库行列值
        protected string GetCellValue(int rowindex, int colindex)
        {

            //获取数据库的行与列
            var control = GV1.Rows[rowindex].Cells[colindex];
            //索引从0开始遍历数据库数据，如果数据>0则进行获取，如果等于0则返回原内容
            var res = control.Controls.Count > 0 ? ((System.Web.UI.WebControls.TextBox)control.Controls[0]).Text : control.Text;
            return res;

        }
        #endregion

        #region 用户登录界面
        protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {

        }
        #endregion

        #region 添加按钮
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            ////新增一个数据表
            //var dt = new DataTable();

            ////新增列的title
            //dt.Columns.Add(new DataColumn("Id"));
            //dt.Columns.Add(new DataColumn("username"));
            //dt.Columns.Add(new DataColumn("password"));
            ////遍历
            //foreach (GridViewRow row in GV1.Rows)
            //{
            //    //将数据赋值给新增行
            //    var tempRow = dt.NewRow();
            //    tempRow["Id"] = GetCellValue(row.RowIndex, 1);
            //    tempRow["Username"] = GetCellValue(row.RowIndex, 2);
            //    tempRow["password"] = GetCellValue(row.RowIndex, 3);

            //    //将数据添加到新增的行
            //    dt.Rows.Add(tempRow);
            //}


            var sql = "select * from Users";
            var dt = DbHelper.GetDataTable(sql);

            dt.Rows.Add(dt.NewRow());

            GV1.DataSource = dt;
            GV1.DataBind();

            GV1.EditIndex = dt.Rows.Count - 1;

            GV1.DataSource = dt;
            GV1.DataBind();

        }
        #endregion

        #region 删除操作
        protected void GV1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            var deleteRow = GV1.Rows.Count; //获取行数
            var Id = GetCellValue(e.RowIndex, 1);
            //var content = GV1.Rows[0].Cells[0];//获取主键
            if (GV1.Rows.Count > 0)
            {

                var res = MessageBox.Show("是否删除？", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (res.Equals(DialogResult.Yes))
                {
                    if (deleteRow > 0)
                    {

                        string sql = string.Format("DELETE FROM Users WHERE id = '{0}'", Id);

                        var dtDelete = DbHelper.AddOrUpdateOrDelete(sql);

                        if (dtDelete != 0)
                        {
                            MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            //填充数据
                            FillData();
                        }
                        else
                        {
                            MessageBox.Show("删除失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                }
                else
                {
                    MessageBox.Show("取消成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }

        }
        #endregion

        #region 更新操作
        protected void GV1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var idString = GetCellValue(e.RowIndex, 1);
            var isOk = int.TryParse(idString, out int id);
            var username = GetCellValue(e.RowIndex, 2);
            var password = GetCellValue(e.RowIndex, 3);
            if (isOk)
            {
                var sql = string.Format("update Users set username = '{0}',password = '{1}' where id = {2}", username, password, id);
                DbHelper.AddOrUpdateOrDelete(sql);
                FillData();
            }
            else
            {
                var sql = string.Format("insert into Users(username,password) values ('{0}','{1}')", username, password);
                DbHelper.AddOrUpdateOrDelete(sql);
                FillData();
            }

            FillData();


        }
        #endregion

        #region 编辑操作
        protected void GV1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GV1.EditIndex = e.NewEditIndex;
            FillData();
        }
        #endregion

        #region 取消编辑操作
        //取消编辑
        protected void GV1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GV1.EditIndex = -1;
            FillData();
        }
        #endregion

        #region 登录按钮
        protected void Button1_Click(object sender, EventArgs e)
        {
            //获取文本框中的值
            string username = this.TextBox1.Text;
            string password = this.TextBox2.Text;

            if (username.Equals("") || password.Equals(""))//用户名或密码为空
            {
                MessageBox.Show("用户名或密码不能为空");
            }
            else
            //用户名与密码不为空
            {
                //到数据库中验证   
                string selectSql = string.Format("select * from Users where username='{0}' and password='{1}'", username, password);
                //返回符合的结果数量
                var count = DbHelper.GetDataTable(selectSql);
                //如果信息>0则说明信息匹配成功
                if (count.Rows.Count > 0)
                {
                    var row = count.Rows[0];
                    //判断账号是否启用
                    var IsActived = (bool)row.ItemArray[4];
                    //判断账号是否删除
                    var IsDeleted = (bool)row.ItemArray[5];
                    //账号已被删除
                    if (IsDeleted)
                    {
                        MessageBox.Show("此账号已被删除，如有疑问，请联系客服");
                    }
                    else
                    {
                        //账号未删除，继续判断
                        if (IsActived)
                        {
                            MessageBox.Show("信息验证成功");
                            //账号已启用，跳转界面
                            Response.Redirect("http://www.baidu.com");
                        }
                        else
                        {
                            //账号已停用
                            MessageBox.Show("账号已停用，如有疑问，请联系客服");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("账号或密码错误，请重新输入！");
                }
            }
        }
        #endregion

        #region 注册按钮
        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("Register.aspx");
        }

        #endregion

    }
    
}




